package com.fashion.web.actions.management;

import com.fashion.model.NewsColumn;
import com.fashion.service.ColumnService;
import com.fashion.utils.ColumnResult;
import com.fashion.utils.ColumnTree;
import com.fashion.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * User: juventus
 * Date: 11-5-3
 * Time: 下午4:11
 */
@Scope("prototype")
@Controller
@ParentPackage("json-default")
@Namespace("/management/columns")
public class ColumnsAction extends ActionSupport {
    private ColumnService columnService;

    private List<NewsColumn> columns;
    private List<ColumnTree> columnTrees;

    private NewsColumn columnForm;
    private String jsonResult;
    private ColumnResult columnResult;
    private boolean deleteSuccess;
    private String id;

    @Action(value = "fetchColumns", results = {@Result(name = SUCCESS, type = "json", params = {"root", "columns"})})
    public String fetchColumns() throws Exception {
        columns = columnService.findAllTreeColumns();

        return SUCCESS;
    }

    @Action(value = "deleteColumn", results = {@Result(name = SUCCESS, type = "json", params = {"root", "deleteSuccess"})})
    public String deleteColumn() throws Exception {
        columnService.deleteColumnById(id);
        deleteSuccess = true;

        return SUCCESS;
    }

    @Action(value = "columnForm", results = {@Result(name = SUCCESS, location = "../../form_result.jsp")})
    public String columnForm() throws Exception {
        columnResult = new ColumnResult();

        String id = columnForm.getId();
        if (null == id || id.isEmpty()) {
            columnForm.setId(StringUtils.createGuid());
            columnForm.setCreateDateTime(new Date());
            columnResult.setEdit(false);
        } else {
            columnResult.setEdit(true);
        }

    //    columnForm.setCreateDateTime(new Date());
        columnService.saveOrUpdateColumn(columnForm);

        columns = new ArrayList<NewsColumn>();
        columns.add(columnForm);
        columnResult.setColumns(columns);
        columnResult.setSuccessful(true);

        JSONObject jsonObject = JSONObject.fromObject(columnResult);
        jsonResult = jsonObject.toString();

        return SUCCESS;
    }

    @Action(value = "columnsIndex", results = {@Result(name = SUCCESS, location = "../columns_index.jsp")})
    public String columnsIndex() throws Exception {
        return SUCCESS;
    }

    @Action(value = "fetchColumnsForTree", results = {@Result(name = SUCCESS, type = "json", params = {"root", "columnTrees"})})
    public String fetchColumnsForTree() throws Exception {
        columnTrees = columnService.convertToTrees(columnService.findAllTreeColumns());

        return SUCCESS;
    }

    public List<NewsColumn> getColumns() {
        return columns;
    }

    public void setColumns(List<NewsColumn> columns) {
        this.columns = columns;
    }

    @Resource(name = "columnService")
    public void setColumnService(ColumnService columnService) {
        this.columnService = columnService;
    }

    public NewsColumn getColumnForm() {
        return columnForm;
    }

    public void setColumnForm(NewsColumn column) {
        this.columnForm = column;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isDeleteSuccess() {
        return deleteSuccess;
    }

    public List<ColumnTree> getColumnTrees() {
        return columnTrees;
    }

    public void setColumnTrees(List<ColumnTree> columnTrees) {
        this.columnTrees = columnTrees;
    }
}
